package com.easemob.chat;

import android.content.ContentValues;
import android.graphics.BitmapFactory.Options;
import android.text.TextUtils;
import com.easemob.EMCallBack;
import com.easemob.analytics.EMMessageCollector;
import com.easemob.analytics.EMTimeTag;
import com.easemob.chat.core.e;
import com.easemob.chat.core.k;
import com.easemob.chat.core.r;
import com.easemob.cloud.CloudOperationCallback;
import com.easemob.cloud.HttpFileManager;
import com.easemob.util.EMLog;
import com.easemob.util.ImageUtils;
import com.easemob.util.PerfUtils;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.json.JSONArray;
import org.json.JSONObject;

class EMSendMessageRunnable
  implements Runnable
{
  private static final String TAG = "sender";
  private static final String PERF = "perf";
  private Chat chat;
  private MultiUserChat muc;
  private EMMessage msg;
  private EMCallBack callback;
  private static final int WAIT_TIME_OUT = 40;
  static Hashtable<String, Object> sendLocks;
  private Object mutex = new Object();
  private static final int WAIT_SEND_TIME_OUT = 60;
  static Hashtable<String, Object> sendMsgLocks;
  private Object sendMutex = new Object();
  private String groupId;
  private boolean connectedBeforeSend = true;
  private int numberOfRetried = 3;
  private static long lastForceReconnectTime = 0L;
  private static ConcurrentLinkedQueue<EMSendMessageRunnable> pendingMsgQueue = new ConcurrentLinkedQueue();
  private static ExecutorService sendThreadPool = Executors.newFixedThreadPool(3);

  public EMSendMessageRunnable(Chat paramChat, EMMessage paramEMMessage, EMCallBack paramEMCallBack)
  {
    this.chat = paramChat;
    this.msg = paramEMMessage;
    this.callback = paramEMCallBack;
  }

  public EMSendMessageRunnable(MultiUserChat paramMultiUserChat, EMMessage paramEMMessage, EMCallBack paramEMCallBack)
  {
    this.muc = paramMultiUserChat;
    this.msg = paramEMMessage;
    this.callback = paramEMCallBack;
  }

  EMSendMessageRunnable(String paramString, EMMessage paramEMMessage, EMCallBack paramEMCallBack)
  {
    this.groupId = paramString;
    this.msg = paramEMMessage;
    this.callback = paramEMCallBack;
  }

  static synchronized void addSendLock(String paramString, Object paramObject)
  {
    if (sendLocks == null)
      sendLocks = new Hashtable();
    sendLocks.put(paramString, paramObject);
  }

  static synchronized void notifySendLock(String paramString)
  {
    if (sendLocks == null)
      return;
    Object localObject1 = sendLocks.remove(paramString);
    if (localObject1 != null)
      synchronized (localObject1)
      {
        localObject1.notify();
      }
  }

  static synchronized void flushPendingQueue()
  {
    EMLog.d("sender", "start flush Pending Queue");
    for (EMSendMessageRunnable localEMSendMessageRunnable = (EMSendMessageRunnable)pendingMsgQueue.poll(); localEMSendMessageRunnable != null; localEMSendMessageRunnable = (EMSendMessageRunnable)pendingMsgQueue.poll())
    {
      EMLog.d("sender", "resend msg : " + localEMSendMessageRunnable.msg.getMsgId());
      sendThreadPool.submit(localEMSendMessageRunnable);
    }
  }

  public void run()
  {
    checkConnection();
    if ((this.msg.getChatType() == EMMessage.ChatType.GroupChat) && (this.muc == null) && (this.groupId != null))
    {
      localObject = EMContactManager.getEidFromGroupId(this.groupId);
      try
      {
        MultiUserChat localMultiUserChat = EMGroupManager.getInstance().getMUCWithoutJoin((String)localObject);
        this.muc = localMultiUserChat;
      }
      catch (Exception localException)
      {
      }
    }
    this.msg.status = EMMessage.Status.INPROGRESS;
    Object localObject = new EMTimeTag();
    ((EMTimeTag)localObject).start();
    switch ($SWITCH_TABLE$com$easemob$chat$EMMessage$Type()[this.msg.type.ordinal()])
    {
    case 1:
    case 4:
    case 7:
      sendMessageXmpp(this.msg);
      break;
    case 2:
      sendImageMessage(this.msg, this.callback);
      break;
    case 5:
      sendFileMessage(this.msg, this.callback);
      break;
    case 6:
      sendFileMessage(this.msg, this.callback);
      break;
    case 3:
      sendVideoMessage(this.msg, this.callback);
      break;
    default:
      EMLog.e("sender", "unsupport msg type, need to check:" + this.msg.type);
    }
    EMMessageCollector.collectSendMsgTime(((EMTimeTag)localObject).stop(), this.msg);
  }

  private void sendMessageXmpp(EMMessage paramEMMessage)
  {
    try
    {
      String str = MessageEncoder.getJSONMsg(paramEMMessage, false);
      EMLog.d("sender", "try to send msg to:" + paramEMMessage.to + " msg:" + str);
      Message localMessage = new Message();
      localMessage.setPacketID(paramEMMessage.getMsgId());
      EMChatOptions localEMChatOptions = EMChatManager.getInstance().getChatOptions();
      if (localEMChatOptions.getUseEncryption())
      {
        str = EMEncryptUtils.encryptMessage(str, paramEMMessage.getTo());
        localMessage.addExtension(new r());
      }
      localMessage.setBody(str);
      if (localEMChatOptions.getRequireServerAck())
        addSendLock(localMessage.getPacketID(), this.mutex);
      this.connectedBeforeSend = EMChatManager.getInstance().isConnected();
      if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
      {
        localMessage.setType(Message.Type.groupchat);
        localMessage.setTo(this.muc.getRoom());
        EMLog.d("sender", "send message to muc:" + this.muc.getRoom());
        this.muc.sendMessage(localMessage);
      }
      else
      {
        this.chat.sendMessage(localMessage);
      }
      if (localEMChatOptions.getRequireServerAck())
      {
        EMLog.d("sender", "wait for server ack...");
        synchronized (this.mutex)
        {
          if (sendLocks.containsKey(localMessage.getPacketID()))
            this.mutex.wait(40000L);
        }
        EMLog.d("sender", "exit from wait");
        if (sendLocks.remove(localMessage.getPacketID()) != null)
        {
          EMLog.e("sender", "did not receive ack from server for msg:" + localMessage.getPacketID());
          if ((this.connectedBeforeSend) && (EMChatManager.getInstance().isConnected()))
          {
            this.numberOfRetried -= 1;
            if (this.numberOfRetried <= 0)
            {
              paramEMMessage.status = EMMessage.Status.FAIL;
              if (paramEMMessage.getType() != EMMessage.Type.CMD)
                updateMsgState(paramEMMessage);
              if (this.callback != null)
                this.callback.onError(-2, "no response from server");
              return;
            }
            pendingMsgQueue.offer(this);
            EMLog.d("sender", "add new msg to pending msg queue : " + paramEMMessage.getMsgId());
            if ((lastForceReconnectTime == 0L) || (System.currentTimeMillis() - lastForceReconnectTime > 30000L))
            {
              lastForceReconnectTime = System.currentTimeMillis();
              EMChatManager.getInstance().forceReconnect();
            }
            else if (EMChatManager.getInstance().isConnected())
            {
              flushPendingQueue();
            }
          }
          else
          {
            paramEMMessage.status = EMMessage.Status.FAIL;
            if (paramEMMessage.getType() != EMMessage.Type.CMD)
              updateMsgState(paramEMMessage);
            if (this.callback != null)
              this.callback.onError(-2, "no response from server");
          }
          return;
        }
      }
      paramEMMessage.status = EMMessage.Status.SUCCESS;
      if (paramEMMessage.getType() != EMMessage.Type.CMD)
        updateMsgState(paramEMMessage);
      if (this.callback != null)
        this.callback.onSuccess();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      if (paramEMMessage.getType() != EMMessage.Type.CMD)
        paramEMMessage.status = EMMessage.Status.FAIL;
      updateMsgState(paramEMMessage);
      if (this.callback != null)
        this.callback.onError(-2, localException.toString());
    }
  }

  private void sendImageMessage(final EMMessage paramEMMessage, final EMCallBack paramEMCallBack)
  {
    File localFile1 = null;
    Object localObject = null;
    final ImageMessageBody localImageMessageBody = (ImageMessageBody)paramEMMessage.body;
    localObject = localImageMessageBody.localUrl;
    if (localObject != null)
    {
      localFile1 = new File((String)localObject);
      if (!localFile1.exists())
      {
        localObject = getThumbnailImagePath((String)localObject);
        localFile1 = new File((String)localObject);
      }
    }
    if ((localFile1 == null) || (!localFile1.exists()))
    {
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-3, "file doesn't exist");
      return;
    }
    boolean bool1 = false;
    if (!localImageMessageBody.isSendOriginalImage())
    {
      String str1 = ImageUtils.getScaledImage(EMChat.getInstance().getAppContext(), (String)localObject);
      if (!str1.equals(localObject))
      {
        EMLog.d("sender", "send scaled image:" + str1);
        bool1 = true;
        localFile1 = new File(str1);
        long l2 = new File((String)localObject).length();
        long l4 = localFile1.length();
        EMLog.d("perf", "original image size:" + l2 + " scaled image size:" + l4 + " ratio:" + (int)(l4 / l2) + "%");
      }
      localObject = str1;
    }
    long l1 = localFile1.length();
    EMLog.d("sender", "start to send file:" + (String)localObject + " size:" + l1);
    final long l3 = System.currentTimeMillis();
    BitmapFactory.Options localOptions = ImageUtils.getBitmapOptions((String)localObject);
    int i = localOptions.outWidth;
    int j = localOptions.outHeight;
    localImageMessageBody.width = i;
    localImageMessageBody.height = j;
    HttpFileManager localHttpFileManager = new HttpFileManager(EMChat.getInstance().getAppContext());
    final String str2 = k.a().y();
    EMLog.d("sender", "remote file path:" + str2);
    boolean bool2 = bool1;
    final File localFile2 = localFile1;
    EMChatOptions localEMChatOptions = EMChatManager.getInstance().getChatOptions();
    if (localEMChatOptions.getUseEncryption())
      localObject = EMEncryptUtils.encryptFile((String)localObject, paramEMMessage.getTo());
    HashMap localHashMap = new HashMap();
    localHashMap.put("restrict-access", "true");
    String str3 = EMChatManager.getInstance().getAccessToken();
    if (TextUtils.isEmpty(str3))
    {
      paramEMMessage.status = EMMessage.Status.FAIL;
      updateMsgState(paramEMMessage);
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-2, "unauthorized token is null");
      return;
    }
    localHashMap.put("Authorization", "Bearer " + str3);
    localHttpFileManager.uploadFile((String)localObject, str2, EMChatConfig.getInstance().APPKEY, null, localHashMap, new CloudOperationCallback()
    {
      public void onProgress(int paramAnonymousInt)
      {
        paramEMMessage.progress = paramAnonymousInt;
        if (paramAnonymousInt == 100)
        {
          long l = System.currentTimeMillis() - l3;
          EMLog.d("perf", "upload " + paramAnonymousInt + "% file size:" + paramEMCallBack + "(bytes) time:" + l + "(ms) speed:" + (int)(paramEMCallBack / l) + "(byte/ms)");
        }
        if (localFile2 != null)
        {
          localFile2.onProgress(paramAnonymousInt, null);
          EMLog.d("sender", "sendfile progress:" + paramAnonymousInt);
        }
      }

      public void onError(String paramAnonymousString)
      {
        EMLog.d("sender", "upload error:" + paramAnonymousString);
        if (str2)
          localImageMessageBody.delete();
        paramEMMessage.status = EMMessage.Status.FAIL;
        EMSendMessageRunnable.this.updateMsgState(paramEMMessage);
        if (localFile2 != null)
        {
          localFile2.onProgress(100, null);
          localFile2.onError(-2, paramAnonymousString);
        }
      }

      public void onSuccess(String paramAnonymousString)
      {
        String str1 = "";
        String str2 = "";
        Object localObject;
        try
        {
          JSONObject localJSONObject = new JSONObject(paramAnonymousString);
          localObject = localJSONObject.getJSONArray("entities").getJSONObject(0);
          str1 = ((JSONObject)localObject).getString("uuid");
          if (((JSONObject)localObject).has("share-secret"))
            str2 = ((JSONObject)localObject).getString("share-secret");
        }
        catch (Exception localException1)
        {
          EMLog.e("sendImageMessage", "json parse exception remotefilepath:" + this.val$remoteFilePath);
        }
        try
        {
          String str3 = "";
          localObject = "";
          String str4 = "";
          EMChatOptions localEMChatOptions = EMChatManager.getInstance().getChatOptions();
          if (localEMChatOptions.getUseEncryption())
          {
            EMLog.d("sender", "start to upload encrypted thumbnail");
            Map localMap = EMSendMessageRunnable.this.uploadEncryptedThumbnailImage(localImageMessageBody, paramEMMessage);
            str3 = (String)localMap.get("uuid");
            localObject = (String)localMap.get("share-secret");
            str4 = k.a().y() + str3;
            EMLog.d("sender", "encryptd thumbnail uploaded to:" + str4);
            if (TextUtils.isEmpty(str3))
            {
              if (localFile2 != null)
              {
                EMLog.e("sender", "upload thumb uuid is null");
                localFile2.onError(-2, "upload file fail ");
              }
              return;
            }
          }
          long l = System.currentTimeMillis() - l3;
          EMLog.d("perf", "uploaded file size:" + paramEMCallBack + "(bytes) time:" + l + "(ms) speed:" + (int)(paramEMCallBack / l) + "(byte/ms)");
          if (TextUtils.isEmpty(str1))
          {
            if (localFile2 != null)
              localFile2.onError(-2, "upload file fail ");
          }
          else
          {
            String str5 = k.a().y() + str1;
            str5 = str5.replaceAll("#", "%23");
            str4 = str4.replaceAll("#", "%23");
            this.val$imgMsgBody.remoteUrl = str5;
            this.val$imgMsgBody.thumbnailUrl = (TextUtils.isEmpty(str4) ? str5 : str4);
            this.val$imgMsgBody.secret = str2;
            this.val$imgMsgBody.thumbnailSecret = ((String)localObject);
            EMSendMessageRunnable.this.sendMessageXmpp(paramEMMessage);
            EMLog.d("sender", "sent msg successfully:" + paramEMMessage.toString());
          }
        }
        catch (Exception localException2)
        {
          localException2.printStackTrace();
          if (localFile2 != null)
          {
            localFile2.onProgress(100, null);
            localFile2.onError(-2, localException2.toString());
          }
        }
        if (str2)
          localImageMessageBody.delete();
      }
    });
  }

  private void sendVideoMessage(final EMMessage paramEMMessage, final EMCallBack paramEMCallBack)
  {
    String str1 = null;
    final VideoMessageBody localVideoMessageBody = (VideoMessageBody)paramEMMessage.body;
    str1 = localVideoMessageBody.localUrl;
    final String str2 = localVideoMessageBody.localThumb;
    File localFile = new File(str1);
    if ((str1 == null) || (!localFile.exists()))
    {
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-3, "video file doesn't exist");
      return;
    }
    if ((str2 == null) || (!new File(str2).exists()))
    {
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-3, "video thumb file doesn't exist");
      return;
    }
    HttpFileManager localHttpFileManager = new HttpFileManager(EMChat.getInstance().getAppContext());
    final String str3 = k.a().y();
    EMLog.d("sender", "remote file path:" + str3);
    EMChatOptions localEMChatOptions = EMChatManager.getInstance().getChatOptions();
    if (localEMChatOptions.getUseEncryption())
      str1 = EMEncryptUtils.encryptFile(str1, paramEMMessage.getTo());
    HashMap localHashMap = new HashMap();
    localHashMap.put("restrict-access", "true");
    String str4 = EMChatManager.getInstance().getAccessToken();
    if (TextUtils.isEmpty(str4))
    {
      paramEMMessage.status = EMMessage.Status.FAIL;
      updateMsgState(paramEMMessage);
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-2, "unauthorized token is null");
      return;
    }
    localHashMap.put("Authorization", "Bearer " + str4);
    localHttpFileManager.uploadFile(str1, str3, EMChatConfig.getInstance().APPKEY, null, localHashMap, new CloudOperationCallback()
    {
      public void onSuccess(String paramAnonymousString)
      {
        String str1 = "";
        String str2 = "";
        Object localObject;
        try
        {
          JSONObject localJSONObject = new JSONObject(paramAnonymousString);
          localObject = localJSONObject.getJSONArray("entities").getJSONObject(0);
          str1 = ((JSONObject)localObject).getString("uuid");
          if (((JSONObject)localObject).has("share-secret"))
            str2 = ((JSONObject)localObject).getString("share-secret");
        }
        catch (Exception localException1)
        {
          EMLog.e("sender", "json parse exception remotefilePath:" + str3);
        }
        try
        {
          String str3 = "";
          localObject = "";
          String str4 = "";
          EMLog.d("sender", "start to upload encrypted thumbnail");
          Map localMap = EMSendMessageRunnable.this.uploadEncryptedThumbnailImage(new File(str2), paramEMMessage);
          str3 = (String)localMap.get("uuid");
          localObject = (String)localMap.get("share-secret");
          str4 = k.a().y() + str3;
          EMLog.d("sender", "thumbail uploaded to:" + str4);
          if (TextUtils.isEmpty(str1))
          {
            if (paramEMCallBack != null)
            {
              paramEMCallBack.onProgress(100, null);
              paramEMCallBack.onError(-2, "upload file fail");
            }
          }
          else if (TextUtils.isEmpty(str3))
          {
            if (paramEMCallBack != null)
            {
              paramEMCallBack.onProgress(100, null);
              paramEMCallBack.onError(-2, "upload file fail");
            }
          }
          else
          {
            String str5 = k.a().y() + str1;
            str5 = str5.replaceAll("#", "%23");
            str4 = str4.replaceAll("#", "%23");
            localVideoMessageBody.remoteUrl = str5;
            localVideoMessageBody.thumbnailUrl = (TextUtils.isEmpty(str4) ? str5 : str4);
            localVideoMessageBody.secret = str2;
            localVideoMessageBody.thumbnailSecret = ((String)localObject);
            EMSendMessageRunnable.this.sendMessageXmpp(paramEMMessage);
            EMLog.d("sender", "sent msg sucessfully:" + paramEMMessage.toString());
          }
        }
        catch (Exception localException2)
        {
          localException2.printStackTrace();
          if (paramEMCallBack != null)
          {
            paramEMCallBack.onProgress(100, null);
            paramEMCallBack.onError(-2, localException2.getMessage());
          }
        }
      }

      public void onProgress(int paramAnonymousInt)
      {
        paramEMMessage.progress = paramAnonymousInt;
        if (paramEMCallBack != null)
          paramEMCallBack.onProgress(paramAnonymousInt, null);
      }

      public void onError(String paramAnonymousString)
      {
        EMLog.d("sender", "upload error:" + paramAnonymousString);
        paramEMMessage.status = EMMessage.Status.FAIL;
        EMSendMessageRunnable.this.updateMsgState(paramEMMessage);
        if (paramEMCallBack != null)
        {
          paramEMCallBack.onProgress(100, null);
          paramEMCallBack.onError(-2, paramAnonymousString);
        }
      }
    });
  }

  private Map<String, String> uploadEncryptedThumbnailImage(File paramFile, EMMessage paramEMMessage)
  {
    final HashMap localHashMap1 = new HashMap();
    int i = 100;
    String str1 = ImageUtils.getThumbnailImage(paramFile.getAbsolutePath(), i);
    EMChatOptions localEMChatOptions = EMChatManager.getInstance().getChatOptions();
    if (localEMChatOptions.getUseEncryption())
      str1 = EMEncryptUtils.encryptFile(str1, paramEMMessage.getTo());
    HttpFileManager localHttpFileManager = new HttpFileManager(EMChat.getInstance().getAppContext());
    String str2 = k.a().y();
    HashMap localHashMap2 = new HashMap();
    localHashMap2.put("restrict-access", "true");
    String str3 = EMChatManager.getInstance().getAccessToken();
    if (!TextUtils.isEmpty(str3))
      localHashMap2.put("Authorization", "Bearer " + str3);
    localHttpFileManager.uploadFile(str1, str2, EMChatConfig.getInstance().APPKEY, null, localHashMap2, new CloudOperationCallback()
    {
      public void onSuccess(String paramAnonymousString)
      {
        EMLog.d("sender", "encrypted thumbnail uploaded");
        String str1 = "";
        String str2 = "";
        try
        {
          JSONObject localJSONObject1 = new JSONObject(paramAnonymousString);
          JSONObject localJSONObject2 = localJSONObject1.getJSONArray("entities").getJSONObject(0);
          str1 = localJSONObject2.getString("uuid");
          if (localJSONObject2.has("share-secret"))
            str2 = localJSONObject2.getString("share-secret");
        }
        catch (Exception localException)
        {
        }
        localHashMap1.put("uuid", str1);
        localHashMap1.put("share-secret", str2);
      }

      public void onError(String paramAnonymousString)
      {
        EMLog.e("sender", "encrypted thumbnail upload error:" + paramAnonymousString);
      }

      public void onProgress(int paramAnonymousInt)
      {
      }
    });
    return localHashMap1;
  }

  private void sendFileMessage(final EMMessage paramEMMessage, final EMCallBack paramEMCallBack)
  {
    File localFile = null;
    String str1 = null;
    final FileMessageBody localFileMessageBody = (FileMessageBody)paramEMMessage.body;
    str1 = localFileMessageBody.localUrl;
    if (str1 != null)
      localFile = new File(str1);
    if ((localFile == null) || (!localFile.exists()))
    {
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-3, "file doesn't exist");
      return;
    }
    EMChatOptions localEMChatOptions = EMChatManager.getInstance().getChatOptions();
    if (localEMChatOptions.getUseEncryption())
      str1 = EMEncryptUtils.encryptFile(str1, paramEMMessage.getTo());
    long l1 = localFile.length();
    EMLog.d("sender", "start to send file:" + str1 + " size:" + l1);
    final long l2 = System.currentTimeMillis();
    HttpFileManager localHttpFileManager = new HttpFileManager(EMChat.getInstance().getAppContext());
    String str2 = k.a().y();
    HashMap localHashMap = new HashMap();
    localHashMap.put("restrict-access", "true");
    String str3 = EMChatManager.getInstance().getAccessToken();
    if (TextUtils.isEmpty(str3))
    {
      paramEMMessage.status = EMMessage.Status.FAIL;
      updateMsgState(paramEMMessage);
      if (paramEMCallBack != null)
        paramEMCallBack.onError(-2, "unauthorized token is null");
      return;
    }
    localHashMap.put("Authorization", "Bearer " + str3);
    localHttpFileManager.uploadFile(str1, str2, EMChatConfig.getInstance().APPKEY, null, localHashMap, new CloudOperationCallback()
    {
      public void onProgress(int paramAnonymousInt)
      {
        paramEMMessage.progress = paramAnonymousInt;
        if (paramAnonymousInt == 100)
        {
          long l = PerfUtils.getTimeSpendSecond(l2);
          EMLog.d("perf", "upload " + paramAnonymousInt + "% file size(byte)" + paramEMCallBack + " time(s)" + l + " speed(byte/s)" + PerfUtils.getSpeed(paramEMCallBack, System.currentTimeMillis() - l2));
        }
        if (localFileMessageBody != null)
          localFileMessageBody.onProgress(paramAnonymousInt, null);
      }

      public void onError(String paramAnonymousString)
      {
        EMLog.d("sender", "upload error:" + paramAnonymousString);
        paramEMMessage.status = EMMessage.Status.FAIL;
        EMSendMessageRunnable.this.updateMsgState(paramEMMessage);
        if (localFileMessageBody != null)
        {
          localFileMessageBody.onProgress(100, null);
          localFileMessageBody.onError(-2, paramAnonymousString);
        }
      }

      public void onSuccess(String paramAnonymousString)
      {
        try
        {
          String str1 = "";
          String str2 = "";
          try
          {
            JSONObject localJSONObject1 = new JSONObject(paramAnonymousString);
            JSONObject localJSONObject2 = localJSONObject1.getJSONArray("entities").getJSONObject(0);
            str1 = localJSONObject2.getString("uuid");
            if (localJSONObject2.has("share-secret"))
              str2 = localJSONObject2.getString("share-secret");
          }
          catch (Exception localException2)
          {
            if ((localException2 != null) && (localException2.getMessage() != null))
              EMLog.d("sendFileMessage", localException2.getMessage());
          }
          if (TextUtils.isEmpty(str1))
          {
            if (localFileMessageBody != null)
            {
              localFileMessageBody.onProgress(100, null);
              localFileMessageBody.onError(-2, "upload file fail");
            }
          }
          else
          {
            String str3 = this.val$remoteFilePath + str1;
            str3 = str3.replaceAll("#", "%23").replaceAll(" ", "%20");
            this.val$fileMsgBody.remoteUrl = str3;
            this.val$fileMsgBody.secret = str2;
            long l = PerfUtils.getTimeSpendSecond(l2);
            EMLog.d("perf", "uploaded file size(bytes)" + paramEMCallBack + " time(s)" + l + " speed(bytes/s)" + PerfUtils.getSpeed(paramEMCallBack, System.currentTimeMillis() - l2));
            EMSendMessageRunnable.this.sendMessageXmpp(paramEMMessage);
            EMLog.d("sender", "sent msg successfully:" + paramEMMessage.toString());
          }
        }
        catch (Exception localException1)
        {
          localException1.printStackTrace();
          if (localFileMessageBody != null)
          {
            localFileMessageBody.onProgress(100, null);
            localFileMessageBody.onError(-2, localException1.toString());
          }
        }
      }
    });
  }

  private void updateMsgState(EMMessage paramEMMessage)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("status", paramEMMessage.status.ordinal());
    e.a().a(paramEMMessage.msgId, localContentValues);
  }

  private String getThumbnailImagePath(String paramString)
  {
    String str = paramString.substring(0, paramString.lastIndexOf("/") + 1);
    str = str + "th" + paramString.substring(paramString.lastIndexOf("/") + 1, paramString.length());
    EMLog.d("msg", "original image path:" + paramString);
    EMLog.d("msg", "thum image path:" + str);
    return str;
  }

  private void checkConnection()
  {
    try
    {
      EMChatManager.getInstance().checkConnection();
    }
    catch (Exception localException)
    {
      synchronized (this.sendMutex)
      {
        addSendMsgLock(this.msg.getMsgId(), this.sendMutex);
        try
        {
          this.sendMutex.wait(60000L);
          EMLog.d("sender", "wait send message time out");
        }
        catch (InterruptedException localInterruptedException)
        {
          localInterruptedException.printStackTrace();
        }
      }
    }
  }

  synchronized void addSendMsgLock(String paramString, Object paramObject)
  {
    if (sendMsgLocks == null)
      sendMsgLocks = new Hashtable();
    sendMsgLocks.put(paramString, paramObject);
  }

  static synchronized void notifySendMsgLocks()
  {
    if ((sendMsgLocks == null) || (sendMsgLocks.size() == 0))
      return;
    Iterator localIterator = sendMsgLocks.values().iterator();
    while (localIterator.hasNext())
    {
      Object localObject1 = localIterator.next();
      synchronized (localObject1)
      {
        localObject1.notify();
      }
    }
    sendMsgLocks.clear();
  }

  static void onInit()
  {
    sendThreadPool = Executors.newFixedThreadPool(3);
  }

  static void onDestroy()
  {
    try
    {
      sendThreadPool.shutdownNow();
      for (EMSendMessageRunnable localEMSendMessageRunnable = (EMSendMessageRunnable)pendingMsgQueue.poll(); localEMSendMessageRunnable != null; localEMSendMessageRunnable = (EMSendMessageRunnable)pendingMsgQueue.poll())
        if (localEMSendMessageRunnable.msg != null)
        {
          localEMSendMessageRunnable.msg.status = EMMessage.Status.FAIL;
          if (localEMSendMessageRunnable.msg.getType() != EMMessage.Type.CMD)
            localEMSendMessageRunnable.updateMsgState(localEMSendMessageRunnable.msg);
        }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMSendMessageRunnable
 * JD-Core Version:    0.6.1
 */